# api配置信息
import json
import os


def get_info():
    home_path = os.getenv('HOME')
    conf_path = os.path.join(home_path, 'conf')
    key_conf = os.path.join(conf_path, 'tkzs_conf')
    with open(key_conf, 'r') as f:
        res_text = f.read()
    info_dict = json.loads(res_text)
    return info_dict


def get_sql_conf():
    home_path = os.getenv('HOME')
    conf_path = os.path.join(home_path, 'conf')
    sql_conf = os.path.join(conf_path, 'sqlconf')
    with open(sql_conf, "r") as f:
        res_text = f.read()
    info_dict = json.loads(res_text)
    return info_dict


def get_miao_info():
    home_path = os.getenv('HOME')
    conf_path = os.path.join(home_path, 'conf')
    key_conf = os.path.join(conf_path, 'miaoquan')
    with open(key_conf, 'r') as f:
        key = f.read()
    return json.loads(key.strip())


def get_version_info():
    home_path = os.getenv('HOME')
    conf_path = os.path.join(home_path, 'conf')
    key_conf = os.path.join(conf_path, 'hhg_version')
    with open(key_conf, 'r') as f:
        key = f.read()
    return json.loads(key.strip())


def get_miyao():
    home_path = os.getenv('HOME')
    conf_path = os.path.join(home_path, 'conf')
    miyao_path = os.path.join(conf_path, 'miyao')
    with open(miyao_path, 'r') as f:
        miyao_dict_str = f.read()
    return json.loads(miyao_dict_str).get("miyao")


def get_wx1_conf():
    home_path = os.getenv('HOME')
    conf_path = os.path.join(home_path, 'conf')
    miyao_path = os.path.join(conf_path, 'wx1_conf')
    with open(miyao_path, 'r') as f:
        miyao_dict_str = f.read()
    return json.loads(miyao_dict_str)


def get_conf(conf_name):
    home_path = os.getenv('HOME')
    conf_path = os.path.join(home_path, 'conf')
    miyao_path = os.path.join(conf_path, conf_name)
    with open(miyao_path, 'r') as f:
        miyao_dict_str = f.read()
    return json.loads(miyao_dict_str)


# redis配置
redis_conf = get_conf("redis_conf")
redis_host = redis_conf.get("host")
redis_port = redis_conf.get("port")

# 数据库相关配置
sql_conf = get_sql_conf()

prefix = "mysql://%s:%s@%s:%s/shop_api" % (sql_conf.get("SqlUser"), sql_conf.get("SqlPassword"),
                                           sql_conf.get("SqlHost"), sql_conf.get("SqlPort"))
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = prefix
SQLALCHEMY_POOL_SIZE = 10  # 数据库连接池大小
SQLALCHEMY_POOL_TIMEOUT = 20  # 数据库链接超时时间，单位秒

TKZS_DICT = get_info()
APP_KEY = TKZS_DICT.get('appkey')
PASSWORD = TKZS_DICT.get('password')
MOBILE = TKZS_DICT.get('mobile')
PID = TKZS_DICT.get('pid')
HOME_PATH = os.getenv('HOME')
GOODS_INDEX_PATH = os.path.join(HOME_PATH, "tmp/goods_index")
SEARCH_RES_PATH = os.path.join(HOME_PATH, 'tmp/search_res')
GOODS_DETAIL_PATH = os.path.join(HOME_PATH, 'tmp/goods_detail')

# 喵有券
MIAO_DICT = get_miao_info()
MIAO_APKEY = MIAO_DICT.get("apkey")
TB_NAME = MIAO_DICT.get('tb_name')

# 版本号
VERSION_DICT = get_version_info()
VERSION_NUM = VERSION_DICT.get("version_num")
SHOP_API_URL = VERSION_DICT.get("shop_api_url")
VERSION_INFO = VERSION_DICT.get("version_info")

# 秘钥
miyao = get_miyao()


# 敏感词
def read_keywords():
    with open("shop/other_files/keywords") as f1:
        text_list = f1.read().splitlines()
    return text_list


mingan_words = read_keywords()

# es配置
es_url = os.getenv("ES_URL")

# app短信验证码相关
message_conf = get_conf("message_conf")
MESSAGE_APP_ID = message_conf.get("MESSAGE_APP_ID")
MESSAGE_APP_KEY = message_conf.get("MESSAGE_APP_KEY")
MESSAGE_TEMPLATE_ID = message_conf.get("MESSAGE_TEMPLATE_ID")
MESSAGE_SMS_SIGN = message_conf.get("MESSAGE_SMS_SIGN")


# 搜索词
def read_hot_words():
    with open("shop/other_files/hot_words") as f1:
        text_list = f1.read().splitlines()
    return text_list


hot_words = read_hot_words()


# ip黑名单
def read_black_ip_list():
    with open("shop/other_files/black_ip_list") as f1:
        text_list = f1.read().splitlines()
    return text_list


black_ip_list = read_black_ip_list()

# ip代理的redis key
proxy_ip_redis_key = "good_ip_port_set"

# ca证书的绝对路径
ca_file_path = "/home/ubuntu/certdata.txt"

# items队列的key
items_url_queue_redis_key = "goods_items_url_list"

# 商品详情图片缓存时间
goods_img_detail_prefix_time = 0

# 微信1配置
wx1_conf_dict = get_wx1_conf()
wx1_appid = wx1_conf_dict.get("AppID")
wx1_appsecret = wx1_conf_dict.get('AppSecret')
wx1_token = wx1_conf_dict.get('Token')
wx1_access_token_redis_key = "wx1_access_token"
